#ifndef __FONTHANDLER_H__
#define __FONTHANDLER_H__


#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_opengl.h"

#define FONT_HEADER 0
#define FONT_TEXT 1
#define FONT_FPS 2
#define FONT_CUTSCENE 3
#define FONT_ALIEN 4
#define FONT_TITLE 5
#define FONT_MAX 6

std::string getDataPath();
GLuint loadTextureColorKey(SDL_Surface *surface, GLfloat *texcoord, int ckr, int ckg, int ckb);
int powerOfTwo(int x);

// TODO: find who the fuck posted this on the gamedev forums and email him...
// loop up if code released to the public domain without license can be
// redistributed under the GPL...
class GLFont
{
private:
	static const int minGlyph = ' ';
	static const int maxGlyph = 126;

	static int initCounter;

	typedef struct
	{
		int minx, maxx;
		int miny, maxy;
		int advance;
		SDL_Surface *pic;
		GLuint tex;
		GLfloat texMinX, texMinY;
		GLfloat texMaxX, texMaxY;
	} glyph;

	int height;
	int ascent;
	int descent;
	int lineSkip;
	glyph glyphs[maxGlyph + 1];

	std::string font_name;
	int pointSize;
	int style;
	float fgRed, fgGreen, fgBlue;
	float bgRed, bgGreen, bgBlue;

	TTF_Font *ttfFont;

	SDL_Color foreground;



public:
	GLFont(std::string name, int pointSize, int style,
	float fgRed, float fgGreen, float fgBlue ): 
	pointSize(pointSize), style(style),
	fgRed(fgRed), fgGreen(fgGreen), fgBlue(fgBlue), ttfFont(NULL)
	{
		font_name = getDataPath();
		font_name += name;
		
		if (0 == initCounter)
		{
			if (TTF_Init() < 0)
			{
				exit(-1);
			}
		}
		initCounter++;
		initFont();
	}

	~GLFont()
	{
		initCounter--;
		if (0 == initCounter)
		{
			TTF_Quit();
		}
	}

	void loadChar(char c);
	void initFont();

	int getLineSkip()
	{
		return lineSkip;
	}

	int getHeight()
	{
		return height;
	}

	void textSize(const char *text, SDL_Rect *r);

	void drawText(const char *text, int x, int y, float size = 1.0f);
};

class FontHandler
{
private:
	GLFont *m_fonts[FONT_MAX];
	
public:
	FontHandler();
	
	GLFont *get( int font )
	{
		if ( font >= 0 && font < FONT_MAX )
		return m_fonts[font];
		return NULL;
	}
	
};


#endif
